package cn.kone.system.controller;


import cn.kone.base.utlis.R;
import cn.kone.system.annotation.PreAuthorize;
import cn.kone.system.domain.Role;
import cn.kone.system.query.RoleQuery;
import cn.kone.system.service.IRoleService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
@Api(tags = "角色接口类")
public class RoleController {
    @Autowired
    private IRoleService roleService;

    /**
     * 查询全部
     *
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询全部角色")
    @PreAuthorize(name = "查询全部角色", sn = "role:findAllRoles",descs = "查询全部角色")
    public R<List<Role>> findAllRoles() {

        List<Role> all = roleService.findAll();
        return R.ok(all);
    }

    /**
     * 查询单个
     *
     * @param id 用户id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个角色")
    @PreAuthorize(name = "查询单个角色", sn = "role:get",descs = "查询单个角色")
    public R<Role> findOneRoles(@PathVariable("id") Long id) {
        Role one = roleService.findOne(id);
        return R.ok(one);
    }

    @PutMapping
    @ApiOperation(value = "新增和修改角色")
    @PreAuthorize(name = "新增和修改角色", sn = "role:addAndUpdateRole",descs = "新增和修改角色")
    public R<Void> addAndUpdateRole(@RequestBody Role role) {
        Long id = role.getId();
        try {
            if (id == null) {
                roleService.addRole(role);
            } else {
                roleService.updateRole(role);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail();
        }
        return R.ok();
    }

    /**
     * 删除单个
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除单个角色")
    @PreAuthorize(name = "删除单个角色", sn = "role:deleteRoles",descs = "删除单个角色")
    public R<Void> deleteRoles(@PathVariable("id") Long id) {
        try {
            roleService.deleteRole(id);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail();
        }
        return R.ok();
    }

    /**
     * 分页查询
     *
     * @param roleQuery
     * @return
     */
    @PostMapping
    @ApiOperation(value = "分页查询角色")
    @PreAuthorize(name = "分页查询角色", sn = "role:pageQuery",descs = "分页查询角色")
    public PageInfo<Role> pageQuery(@RequestBody RoleQuery roleQuery) {

        List<Role> list = roleService.pageQuery(roleQuery);
        return new PageInfo<Role>(list);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除角色")
    @PreAuthorize(name = "批量删除角色", sn = "role:batchDeleteRole",descs = "批量删除角色")
    public R<Void> batchDeleteRole(@RequestBody Long[] ids) {
        try {
            roleService.batchDeleteRole(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail();
        }
        return R.ok();
    }

}
